package com.ruoyi.project.system.leave.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.forApp.domain.LeaveForApp;
import com.ruoyi.project.system.course.domain.CourseArrange;
import com.ruoyi.project.system.course.mapper.CourseArrangeMapper;
import com.ruoyi.project.system.course.mapper.CourseMapper;
import com.ruoyi.project.system.dept.mapper.DeptMapper;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.leave.mapper.LeaveMapper;
import com.ruoyi.project.system.leave.domain.Leave;
import com.ruoyi.project.system.leave.service.ILeaveService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.util.AutoPopulatingList;

/**
 * 学生请假 服务层实现
 * 
 * @author Waynnn
 * @date 2019-04-07
 */
@Service
public class LeaveServiceImpl implements ILeaveService 
{
	@Autowired
	private LeaveMapper leaveMapper;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private CourseMapper courseMapper;

	@Autowired
	private DeptMapper deptMapper;

	@Autowired
	private CourseArrangeMapper courseArrangeMapper;

	@Autowired
	private ILeaveService leaveService;

	/**
     * 查询学生请假信息
     * 
     * @param leaveId 学生请假ID
     * @return 学生请假信息
     */
    @Override
	public Leave selectLeaveById(Long leaveId)
	{
		User user= ShiroUtils.getSysUser();
		Leave leave=leaveMapper.selectLeaveById(leaveId);
		leave.setStudent(userMapper.selectUserById(leave.getStudentId()));
		leave.setCourse(courseMapper.selectCourseById(leave.getCourseId()));
		leave.setApprover(userMapper.selectUserById(user.getUserId()));
		leave.setStudentClass(deptMapper.selectDeptById(leave.getDeptId()));
	    return leave;

	}
	
	/**
     * 查询学生请假列表
     * 
     * @param leave 学生请假信息
     * @return 学生请假集合
     */
	@Override
	public List<Leave> selectLeaveList(Leave leave)
	{
		User user= ShiroUtils.getSysUser();
		leave.setApproverId(user.getUserId());
		List<Leave> leaveList=leaveMapper.selectLeaveList(leave);
		for (Leave leave1:leaveList){
				leave1.setStudent(userMapper.selectUserById(leave1.getStudentId()));
				leave1.setCourse(courseMapper.selectCourseById(leave1.getCourseId()));
				leave1.setApprover(userMapper.selectUserById(user.getUserId()));
				leave1.setStudentClass(deptMapper.selectDeptById(leave1.getDeptId()));
		}
	    return leaveList;
	}

	/**
	 * 查询学生请假列表
	 *
	 * @param leave 学生请假信息
	 * @return 学生请假集合
	 */
	@Override
	public List<Leave> selectLeaveListForAdmin(Leave leave)
	{
		List<Leave> leaveList=leaveMapper.selectLeaveList(leave);
		for (Leave leave1:leaveList){
			leave1.setStudent(userMapper.selectUserById(leave1.getStudentId()));
			leave1.setCourse(courseMapper.selectCourseById(leave1.getCourseId()));
			leave1.setStudentClass(deptMapper.selectDeptById(leave1.getDeptId()));
		}
		return leaveList;
	}

	@Override
	public List<LeaveForApp> selectLeaveListForApp(Leave leave)
	{
		List<Leave> leaveList=leaveMapper.selectLeaveList(leave);
		for (Leave leave1:leaveList){
			leave1.setStudent(userMapper.selectUserById(leave1.getStudentId()));
			leave1.setCourse(courseMapper.selectCourseById(leave1.getCourseId()));
			leave1.setStudentClass(deptMapper.selectDeptById(leave1.getDeptId()));
		}

		List<LeaveForApp> leaveForApps=new ArrayList<>();

		for (Leave leave1:leaveList){
			LeaveForApp leaveForApp=new LeaveForApp();
			leaveForApp.setCourseId(leave1.getCourseId());
			leaveForApp.setLeaveId(leave1.getLeaveId());
			leaveForApp.setStudentId(leave1.getStudentId());
			leaveForApp.setApprovalId(leave1.getApproverId());
			leaveForApp.setCourseName(leave1.getCourse().getCourseName());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String leaveTime="";
			if (leave1.getLeaveTime()!=null){
				leaveTime=sdf.format(leave1.getLeaveTime());
			}
			leaveForApp.setLeaveTime(leaveTime);
			leaveForApp.setReason(leave1.getReason());
			leaveForApp.setApprovalStatus(leave1.getApprovalStatus());
			leaveForApps.add(leaveForApp);
		}

		return leaveForApps;
	}
	
    /**
     * 新增学生请假
     * 
     * @param leave 学生请假信息
     * @return 结果
     */
	@Override
	public int insertLeave(Leave leave)
	{
		leave.setCreateTime(new Date());
	    return leaveMapper.insertLeave(leave);
	}
	
	/**
     * 修改学生请假
     * 
     * @param leave 学生请假信息
     * @return 结果
     */
	@Override
	public int updateLeave(Leave leave)
	{
		leave.setUpdateTime(new Date());
	    return leaveMapper.updateLeave(leave);
	}

	/**
     * 删除学生请假对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteLeaveByIds(String ids)
	{
		return leaveMapper.deleteLeaveByIds(Convert.toStrArray(ids));
	}
	
}
